﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Text;
using System.Web.Services;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class FuntionGroup : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindFunctionList();
                BindGridView();
            }
        }


        #region Private Method
        private void BindFunctionList()
        {
            FunctionesService serviceObj = ServiceFactory.getFunctionesService();

            OperationResult<List<tb_FunctionItem>> result = serviceObj.getAllFunctionList();

            if (result.operationSuccess)
            {
                cbListFunction.DataSource = result.obj;
                cbListFunction.DataTextField = "Name";
                cbListFunction.DataValueField = "Id";
                cbListFunction.DataBind();
            }
            else
            {
                //EXCEPTION 
                //return null;
            }
        }

        private void BindGridView()
        {
            FunctionGroupService functionGroupServiceObj = ServiceFactory.getFunctionGroupService();
            int totalPage = 0;
            OperationResult<List<tb_FunctionGroup>> result = functionGroupServiceObj.getAllFunctionList(1, 10, out   totalPage);

            if (result.operationSuccess)
            {
                gvFunctionGroup.DataSource = result.obj;
                gvFunctionGroup.DataBind();
            }
            else
            {
                //EXCEPTION 
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {

            FunctionesService serviceObj = ServiceFactory.getFunctionesService();

            OperationResult<List<tb_FunctionItem>> result = serviceObj.getAllFunctionList();

            if (result.operationSuccess)
            {
                cbEditListFunction.DataSource = result.obj;
                cbEditListFunction.DataTextField = "Name";
                cbEditListFunction.DataValueField = "Id";
                cbEditListFunction.DataBind();
            }
            else
            {
                //EXCEPTION 
                //return null;
            }
            FunctionGroupService functionGroupserviceObj = ServiceFactory.getFunctionGroupService();
            OperationResult<List<tb_FunctionGroup>> resultEdit = functionGroupserviceObj.getFunctionGroupById(id);
            if (result.operationSuccess == true)
            {
                txtEditGroupName.Text = resultEdit.obj[0].GroupName;
                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj[0].Id.ToString();
                lbLastModifyDate.Text = result.obj[0].ModifyDate.ToShortDateString();
                OperationResult<List<tb_Mapping_FunctionGroup>> mappingResult = functionGroupserviceObj.getFunctionGroupMappingByGroupId(id);
                if (mappingResult.operationSuccess)
                {

                    foreach (ListItem cb in cbEditListFunction.Items)
                    {
                        foreach (tb_Mapping_FunctionGroup entity in mappingResult.obj)
                        {
                            if (entity.FunctionItemId.ToString() == cb.Value)
                            {
                                cb.Selected = true;
                            }
                        }
                    }
                }
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString(); 
        }
        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            FunctionGroupService functionGroupserviceObj = ServiceFactory.getFunctionGroupService();

            tb_FunctionGroup entity = new tb_FunctionGroup();

            entity.GroupName = this.txtGroupName.Text;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_FunctionGroup> funtionGroupResult = functionGroupserviceObj.craeteFunctionGroup(entity);

            if (funtionGroupResult.operationSuccess)
            {
                List<tb_Mapping_FunctionGroup> mappingEntityList = new List<tb_Mapping_FunctionGroup>();

                foreach (ListItem cb in cbListFunction.Items)
                {
                    if (cb.Selected == true)
                    {
                        tb_Mapping_FunctionGroup mappingEntity = new tb_Mapping_FunctionGroup();

                        mappingEntity.FunctionGroupId = funtionGroupResult.obj.Id;
                        mappingEntity.FunctionItemId = Int32.Parse(cb.Value);

                        mappingEntityList.Add(mappingEntity);

                        functionGroupserviceObj.createFunctionGroupMapping(mappingEntity);
                    }
                }
            }
            BindGridView();
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }
            FunctionGroupService functionGroupserviceObj = ServiceFactory.getFunctionGroupService();

            tb_FunctionGroup entity = new tb_FunctionGroup();

            entity.Id = editId;
            entity.GroupName = this.txtEditGroupName.Text;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_FunctionGroup> funtionGroupResult = functionGroupserviceObj.modifyFunctionGroup(entity);
            if (funtionGroupResult.operationSuccess)
            {
                List<tb_Mapping_FunctionGroup> mappingEntityList = new List<tb_Mapping_FunctionGroup>();

                functionGroupserviceObj.deleteFunctionGroupMapping(editId);
                foreach (ListItem cb in cbEditListFunction.Items)
                {
                    if (cb.Selected == true)
                    {
                        tb_Mapping_FunctionGroup mappingEntity = new tb_Mapping_FunctionGroup();

                        mappingEntity.FunctionGroupId = funtionGroupResult.obj.Id;
                        mappingEntity.FunctionItemId = Int32.Parse(cb.Value);

                        mappingEntityList.Add(mappingEntity);

                        functionGroupserviceObj.createFunctionGroupMapping(mappingEntity);
                    }
                }
            }
            //this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alter('sa')", true);
            BindGridView();

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            FunctionGroupService functionGroupserviceObj = ServiceFactory.getFunctionGroupService();

            functionGroupserviceObj.deleteFunctionGroupMapping(deleteId);
            tb_FunctionGroup deleteEntity = new tb_FunctionGroup();
            deleteEntity.Id = deleteId;
            functionGroupserviceObj.deleteFunctionGroup(deleteEntity);
            BindGridView();

        }

        protected void gvFunctionGroup_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {

                string strSelectId = e.CommandArgument.ToString();

                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }


        protected void gvFunctionGroup_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvFunctionGroup.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        #endregion


    }
}
